package com.example.qxfw.dlqx.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.zy.*;
import com.example.qxfw.dlqx.service.MaxPowerLoadFcstService;
import com.example.qxfw.dlqx.service.MaxPowerLoadService;
import com.example.qxfw.dlqx.service.PowerLoadHourFcstService;
import com.example.qxfw.dlqx.service.PowerLoadHourService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

@ApiIgnore
@Api(tags = "电力气象-电力负荷查询")
@RestController
@CrossOrigin
@RequestMapping("/dlfhcx")
public class DlfhcxController {

    @Autowired
    private PowerLoadHourService powerLoadHourService;

    @Autowired
    private MaxPowerLoadService maxPowerLoadService;

    @Autowired
    private PowerLoadHourFcstService powerLoadHourFcstService;

    @Autowired
    private MaxPowerLoadFcstService maxPowerLoadFcstService;

    @ApiOperation("获取电力逐小时负荷实况最新时间")
    @GetMapping("/getMaxTime")
    public SysResult getMaxTime(){
        List<PowerLoadHour> maxTime = powerLoadHourService.getMaxTime();
        return SysResult.success(maxTime);
    }

    @ApiOperation("获取最大电力负荷实况最新时间")
    @GetMapping("/getMaxTime1")
    public SysResult getMaxTime1(){
        List<MaxPowerLoad> maxTime = maxPowerLoadService.getMaxTime();
        return SysResult.success(maxTime);
    }

    @ApiOperation("获取小时负荷实况")
    @GetMapping("/getPowerLoadHour")
    public SysResult getPowerLoadHour(String dateChar1,String dateChar2){
        List<PowerLoadHour> powerLoadHours = powerLoadHourService.listByParams1(dateChar1, dateChar2);
        return SysResult.success(powerLoadHours);
    }

    @ApiOperation("获取小时负荷预报")
    @GetMapping("/getPowerLoadHourFcst")
    public SysResult getPowerLoadHourFcst(String dateChar1,String dateChar2){
        List<PowerLoadHourFcst> powerLoadHourFcsts = powerLoadHourFcstService.listByParams(dateChar1, dateChar2);
        return SysResult.success(powerLoadHourFcsts);
    }

    @ApiOperation("获取小时电力负荷检测预报")
    @GetMapping("/getPowerLoadHourAll")
    public SysResult getPowerLoadHourAll(String dateChar1,String dateChar2){
        List<PowerLoadHour> powerLoadHours = powerLoadHourService.listByParams1(dateChar1, dateChar2);
        List<PowerLoadHourFcst> powerLoadHourFcsts = powerLoadHourFcstService.listByParams(dateChar1, dateChar2);
        List<PowerLoadHourAll> powerLoadHourAlls = new ArrayList<>();
        for (int i = 0; i < powerLoadHours.size(); i++ ) {
            PowerLoadHour powerLoadHour = powerLoadHours.get(i);
            PowerLoadHourAll powerLoadHourAll = new PowerLoadHourAll();
            powerLoadHourAll.setDateChar(powerLoadHour.getDatechar());
            powerLoadHourAll.setSkLoad(powerLoadHour.getLoad());
            for (PowerLoadHourFcst powerLoadHourFcst : powerLoadHourFcsts) {
                if (powerLoadHour.getDatechar().compareTo(powerLoadHourFcst.getDatechar())==0){
                    powerLoadHourAll.setYbLoad(powerLoadHourFcst.getLoad());
                    powerLoadHourAll.setDiffLoad(powerLoadHour.getLoad().subtract(powerLoadHourFcst.getLoad()));
                    powerLoadHourAlls.add(powerLoadHourAll);
                }
            }
        }
        return SysResult.success(powerLoadHourAlls);
    }

    @ApiOperation("获取日电力负荷实况")
    @GetMapping("/getMaxPowerLoad")
    public SysResult getMaxpowerLoad(String dateChar1,String dateChar2){
        List<MaxPowerLoad> maxPowerLoads = maxPowerLoadService.listByParams(dateChar1, dateChar2);
        return SysResult.success(maxPowerLoads);
    }

    @ApiOperation("获取日电力负荷预报")
    @GetMapping("/getMaxPowerLoadFcst")
    public SysResult getMaxPowerLoadFcst(String dateChar1,String dateChar2){
        List<MaxPowerLoadFcst> maxPowerLoadFcsts = maxPowerLoadFcstService.listByParams(dateChar1, dateChar2);
        return SysResult.success(maxPowerLoadFcsts);
    }

    @ApiOperation("获取日电力负荷检测")
    @GetMapping("/getMaxPowerLoadAll")
    public SysResult getMaxPowerLoadFcstAll(String dateChar1,String dateChar2){
        List<MaxPowerLoad> maxPowerLoads = maxPowerLoadService.listByParams(dateChar1, dateChar2);
        List<MaxPowerLoadFcst> maxPowerLoadFcsts = maxPowerLoadFcstService.listByParams(dateChar1, dateChar2);
        List<MaxPowerLoadAll> maxPowerLoadAlls = new ArrayList<>();
        for (int i = 0; i<maxPowerLoads.size(); i++){
            MaxPowerLoad maxPowerLoad = maxPowerLoads.get(i);
            MaxPowerLoadAll maxPowerLoadAll = new MaxPowerLoadAll();
            maxPowerLoadAll.setDateChar(maxPowerLoad.getDatechar());
            maxPowerLoadAll.setSkDayLoad(maxPowerLoad.getDayload());
            maxPowerLoadAll.setSkDayMaxLoad(maxPowerLoad.getDaymaxload());
            maxPowerLoadAll.setSkDayMinLoad(maxPowerLoad.getDayminload());
            for (MaxPowerLoadFcst powerLoadFcst : maxPowerLoadFcsts) {
                if (maxPowerLoad.getDatechar().compareTo(powerLoadFcst.getDatechar())==0){
                    maxPowerLoadAll.setYbDayLoad(powerLoadFcst.getDayload());
                    maxPowerLoadAll.setYbDayMaxLoad(powerLoadFcst.getDaymaxload());
                    maxPowerLoadAll.setYbDayMinLoad(powerLoadFcst.getDayminload());
                    maxPowerLoadAll.setDiffDayLoad(maxPowerLoad.getDayload().subtract(powerLoadFcst.getDayload()));
                    maxPowerLoadAll.setDiffDayMaxLoad(maxPowerLoad.getDaymaxload().subtract(powerLoadFcst.getDaymaxload()));
                    maxPowerLoadAll.setDiffDayMinLoad(maxPowerLoad.getDayminload().subtract(powerLoadFcst.getDayminload()));
                    maxPowerLoadAlls.add(maxPowerLoadAll);
                }
            }


        }
        return SysResult.success(maxPowerLoadAlls);
    }
}
